< previous page page_247 next page >

Page 247
Once the program has calculated the acceleration components for an object, it adds the components to the object's velocity. It can then use the object's velocity to compute its new position.
Example program Planets uses these equations to simulate planetary motion. Select the File menu's Open command to load a file containing object data. The file should begin with the number of planets defined by the file. Then for each planet, the file should contain the planet's initial position, velocity, mass, and the color the program should use when drawing it.
For example, the following sample values describe two planets. The first begins centered at position (100, 175) with velocity components 0 in the X direction and 8 in the Y direction. It has mass 20 and color &H880000&, which is dark blue. The Ch9 directory on the CD-ROM contains several planet data files with .pla extensions.
e10a6a0a59f035e870d90cca05b85fce.gif
2
100 175 0 -8 20 &H880000&
300 175 0  8 20 &H008800&
The following Visual Basic code shows the main animation subroutine used by program Planets.
e10a6a0a59f035e870d90cca05b85fce.gif
' Make the planets move until Playing is false.
Private Sub PlayImages(ByVal ms_per_frame As Long)
Const F_SCALE = 1000

Dim next_time As Long
Dim i As Integer
Dim j As Integer
Dim dx As Double
Dim dy As Double
Dim d2 As Double
Dim d As Double
Dim f As Double
Dim a_d As Double

    ' Start the animation. 
    next_time = GetTickCount() 
    Do While Playing 
        ' Calculate the forces on the planets. 
        For i = 1 To NumPlanets - 1 
            For j = i + 1 To NumPlanets 
                ' Calculate the force between planets 
                ' i and j. Translate the force into a 
                ' change in velocity. 
                dx = Cx(i) - Cx(j) 
                dy = Cy(i) - Cy(j) 
                d2 = dx * dx + dy * dy 
                f = F_SCALE * M(i) * M(j) / d2 
                d = Sqr(d2)

 
< previous page page_247 next page >